package com.hzhc.controller.baseinfo;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.YytNotice;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.YytNoticeService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.YytNoticeVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 新闻资讯管理
 * @author zhoumx
 * @description 新闻资讯管理
 * @date 2024/5/25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/yytNotice")
public class YyNoticeController extends BaseController {
    private final YytNoticeService yytNoticeService;

    @Value("${file.yypUploadFolder}")
    private String yypUploadFolder;
    @Value("${file.yypAccessPath")
    private String yypAccessPath;


    @Value("${file.zcUploadFolder}")
    private String zcUploadFolder;
    @Value("${file.zcAccessPath")
    private String zcAccessPath;
    /**
     * 获取新闻资讯管理分页列表
     */
    @RequestMapping("/getYytNoticeVoList")
    public ResultUtil<List<YytNoticeVo>> getYyNoticeVoList(@RequestBody YytNoticeVo yytNoticeVo){
        Page<YytNoticeVo> page=new Page<YytNoticeVo>(yytNoticeVo.getPageIndex(),yytNoticeVo.getPageSize());
        List<YytNoticeVo> list=yytNoticeService.getYytNoticeVoList(page,yytNoticeVo);
        return ResultUtil.build(list, page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }
    /**
     * 检测新闻资讯管理公司标题
     */
    @RequestMapping("/checkYyNoticeTitle")
    public ResultUtil<T> checkYyNoticeTitle(@RequestBody JSONObject data){
        Long id =null;
        if (data.containsKey("id")){
            id = data.getLong("id");
        }
        boolean bool = yytNoticeService.checkYytNoticeTitle("title", data.getString("title"),id);
        if (bool){
            return ResultUtil.build(ResultUtilCodeEnum.TITLE_ALREAD_EXIST);
        }
            return ResultUtil.ok();
    }
    /**
     * 新闻资讯管理新增
     */
    @PostMapping("/addYyNotice")
    @SysTemLog(operModul = "新闻资讯管理-新闻资讯新增", operType = "新增", operDesc = "新闻资讯新增功能", modular = "PC")
    public ResultUtil<T> addYyNotice(HttpServletRequest request,@RequestBody YytNotice yytNotice){
        boolean bool =yytNoticeService.checkYytNoticeTitle("title",yytNotice.getTitle(),null);
        if (bool){
            return ResultUtil.build(ResultUtilCodeEnum.TITLE_ALREAD_EXIST);
        }
        UserDto user= TokenUtil.getUser(request);
        yytNotice.setUserId(user.getId()).setUserName(user.getRealname());
        yytNotice.setType(0);
        bool = yytNoticeService.addYytNotice(yytNotice);
        return bool ? ResultUtil.ok() : ResultUtil.fail();
    }
    /**
     * 新闻资讯管理修改
     */
    @PostMapping("/updateYytNotice")
    @SysTemLog(operModul = "新闻资讯管理-新闻资讯修改", operType = "修改", operDesc = "新闻资讯修改功能", modular = "PC")
    public ResultUtil<T> updateYytNotice(HttpServletRequest request, @RequestBody YytNotice yytNotice) {
        Boolean bool = yytNoticeService.checkYytNoticeTitle("title", yytNotice.getTitle(), yytNotice.getId());
        if (bool) {
            return ResultUtil.build(ResultUtilCodeEnum.TITLE_ALREAD_EXIST);
        }
        Long userId = TokenUtil.getUserID(request);
        yytNotice.setUpdateUserId(userId);
        bool = yytNoticeService.updateYytNotice(yytNotice);
        return bool ? ResultUtil.ok() : ResultUtil.fail();
    }
    /**
     * 新闻资讯管理置顶
     */
    @PostMapping("/updateYytNoticeTop")
    @SysTemLog(operModul = "新闻资讯管理-新闻资讯置顶", operType = "修改", operDesc = "新闻资讯置顶功能", modular = "PC")
    public ResultUtil<T> updateYytNoticeTop(@RequestBody YytNotice yytNotice) {
        Boolean bool = yytNoticeService.updateYytNoticeTop(yytNotice);
        return bool ? ResultUtil.ok() : ResultUtil.fail();
    }
    /**
     * 新闻资讯管理删除
     */
    @PostMapping("/delYytNotice/{id}")
    @SysTemLog(operModul = "新闻资讯管理-新闻资讯删除", operType = "删除", operDesc = "新闻资讯删除功能", modular = "PC")
    public ResultUtil<T> delYyNotice(@PathVariable("id") Long id){
        Boolean bool = yytNoticeService.delYytNotice(id);
        return bool ? ResultUtil.ok() : ResultUtil.fail();
    }
    /**
     * 新闻资讯管理上传文件
     */
    @PostMapping(path = "/uploadFile", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultUtil<String> uploadFile(HttpServletRequest request,
                                         @RequestParam(value = "file", required = false) MultipartFile file) {

        return yytNoticeService.upfile(file, yypUploadFolder, yypAccessPath);
    }


}






